#include "task_app_logic.h"

static TaskHandle_t thread_app_dazzling;

void brighness_recover() //亮度恢复
{
  u8 brigh_level;
  /*
  if(driver_nvs_part_read_u8(BRIGH_LEVEL,&brigh_level) == ESP_OK) //恢复RGB灯带灯带亮度
  {
    init_brighness_level(brigh_level);
  }
  else
  */
  {
    init_brighness_level(LEVEL);//预设亮度
  }
}

void cw_mode_config_recover()
{
    u8 cw,brighness;
    /*
    if(driver_nvs_part_read_u8(CW_LEVEL,&cw) == ESP_OK) //恢复cw值
    {
      printf("cw level is %d\n",cw);
    }
    else
    */
    {
      cw = 50;
    }
    
    /*
    if(driver_nvs_part_read_u8(CW_BRIGH,&brighness) == ESP_OK) //恢复cw值
    {
      printf("cw level is %d\n",brighness);
    }
    else
    */
    {
      brighness = 50;
    }

    init_cw_level(cw,brighness);
}

void scene_recover()//恢复场景子状态
{
  u8 scene_mdoe;
  /*
  if(driver_nvs_part_read_u8(MODESCENE,&scene_mdoe) == ESP_OK) 
  {
    set_scene_ssp(scene_mdoe);
  }
  
  else
  */
  {
    set_scene_ssp(MODE1);
  }
  set_scene_last_ssp(SCENE_ZERO);
}

void micro_about_recover() //麦克风恢复
{
  u8 micro_senst;
  u8 spectral_mode;
  /*
  if (driver_nvs_part_read_u8(MICROSENST,&micro_senst) == ESP_OK) //恢复灵敏度
  {
    set_senst_level(micro_senst);
  }
  else
  */
  {
    set_senst_level(100);
  }
  /*
  if(driver_nvs_part_read_u8(SPECTRALMODE,&spectral_mode) == ESP_OK) //恢复子状态
  {
    set_micro_ssp(spectral_mode);
  }
  else
  */
  {
    set_micro_ssp(MICRO_ENERGY);
  }
}

void diretion_about_recover() //方向控制
{
    set_light_diretion(FORWARD); //默认正向循环播放灯效
}


u8 app_logic_init()
{
    app_logic_set_ssp(SSP_LOGO); //初始化进入灯光监测
    app_logic_set_last_ssp(SSP_NONE); 
    set_light_length(LED_NUMBER); //设置长度
    brighness_recover(); //灯光亮度初始化
    scene_recover();//恢复场景子状态
    micro_about_recover(); //恢复麦克风模式
    cw_mode_config_recover(); //CW模式色温亮度恢复
    return 0;
}

void app_logic_general_handle(SYS_OPT_KEY opt)
{
  if(app_logic_get_ssp() == SSP_MODE_CW)
  {
    cw_brilliance_control_handle(opt);
  }
  else if ((app_logic_get_ssp() != SSP_MODE_CW) && (app_logic_get_ssp() != SSP_POWER_OFF) && (app_logic_get_ssp() != SSP_LOGO)) //在CW 关机 自检模式下无法进行亮度调节
  {
    brilliance_control_handle(opt); //亮度调节处理函数
  }
  mode_handle(opt);

}




void app_logic_loop(void *pvParameters)
{   
    key_act_param key_act;
    SYS_OPT_KEY opt;
    app_logic_init();
    while (1)
    {   
        opt.oepation = OPT_NONE;
        if(xQueueReceive(sensor_dect_queue,&key_act,0)) //获取按键状态
        {
            LOG_INFO("app logic task : recv msg,type is %d,act_source num is %d\n",key_act.drvAct,key_act.act_source); //接收到按键操作动作
            opt=app_logic_act_to_opt(key_act); //将按键状态转换为操作状态

        }
        app_logic_general_handle(opt);
        vTaskDelay(pdMS_TO_TICKS(EXAMPLE_CHASE_SPEED_MS)); //延时10ms
    }
}

void task_app_logic_init()
{
    xTaskCreate(app_logic_loop,"app_logic_loop",APP_LOGIC_SIZE,NULL,TASK_PROP_APP,&thread_app_dazzling);
}